package com.test.vote.controller;

import com.test.vote.domain.Top;
import com.test.vote.pojo.*;
import com.test.vote.response.CodeMsg;
import com.test.vote.response.ResultData;
import com.test.vote.service.*;
import org.aspectj.apache.bcel.classfile.Code;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
public class voteController {
    @Autowired
    voteService voteService;
    @Autowired
    battleService battleService;
    @Autowired
    playerService playerService;
    @Autowired
    matchService matchService;

    //用户投票
    @PostMapping("/vote")
    @ResponseBody
    public ResultData vote(Vote vote) {
        //判断该用户是否已经投过
        if (voteService.ifVote(vote) != null) {
            return ResultData.error(CodeMsg.VOTE_ERROR);
        }
        voteService.vote(vote);
        //投票成功，对战的对应选手票数加1;
        Battle battle = battleService.selectBattleById(vote.getBattleId());
        if (vote.getPlayerId() == battle.getPlayeraId()) {
            battle.setPlayeraTicket(battle.getPlayeraTicket() + 1);
            voteService.addAVote(battle.getBattleId());
        } else {
            battle.setPlayeraTicket(battle.getPlayerbTicket() + 1);
            voteService.addBVote(battle.getBattleId());
        }
        return ResultData.success(true);
    }

    //根据比赛的Id查看比赛的对战胜方排行
    @RequestMapping("/top")
    public String top(int matchId, Model model) {
        //存储比赛信息
        Match match = matchService.selectMatchById(matchId);
        //存储比赛当中每个对战的最高票方
        List<Map<String, Integer>> maps = voteService.selctTop(match);
        //存储排名信息
        List<Top> tops = new ArrayList<>();
        for (int i = 0; i < maps.size(); i++) {
            //循环取出排名
            Map<String, Integer> map = maps.get(i);
            //对战的最高票数
            int top = map.get("top");
            //对战的Id
            int battleId = map.get("battle_id");
            //根据对战Id找到对战信息
            Battle battle = battleService.selectBattleById(battleId);
            //如果是a选手的票数等于最高票数
            if (battle.getPlayeraTicket() == top) {
                //通过a选手Id找到选手信息
                Player player = playerService.selectPlayerById(battle.getPlayeraId());
                //存储排名
                Top top1 = new Top();
                top1.setTop(i + 1);
                top1.setTicket(top);
                top1.setPlayerId(player.getPlayerId());
                top1.setPlayerImg(player.getPlayerImg());
                top1.setPlayerName(player.getPlayerName());
                top1.setPlayerNumber(player.getPlayerNumber());
                tops.add(top1);
            }
            //如果是a选手的票数等于最高票数
            else if (battle.getPlayerbTicket() == top) {
                //通过b选手Id找到选手信息
                Player player = playerService.selectPlayerById(battle.getPlayerbId());
                //存储排名
                Top top1 = new Top();
                top1.setPlayerId(player.getPlayerId());
                top1.setPlayerImg(player.getPlayerImg());
                top1.setPlayerName(player.getPlayerName());
                top1.setPlayerNumber(player.getPlayerNumber());
                top1.setTop(i + 1);
                top1.setTicket(top);
                tops.add(top1);
            }
        }
        model.addAttribute("top", tops);
        model.addAttribute("match", match);
        return "matchAdmin/top";
    }
}
